package com.v2future.sjt.collegecheckingattendance.leave;

import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.leave.adapter.CancelLeaveAdapter;
import com.v2future.sjt.collegecheckingattendance.leave.bean.WaitApproveBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.io.Serializable;
import java.util.List;

/**
 * Created by minnie on 16/3/2.
 */
public class CancelLeaveActivity extends MainActivity implements PullToRefreshView.OnHeaderRefreshListener,
        PullToRefreshView.OnFooterRefreshListener, CancelLeaveAdapter.CancelBtnClickListener{

    List<WaitApproveBean> dataList;

    CancelLeaveAdapter adapter;

    PullToRefreshView pullView;

    private ListView cancelLeaveLv;

    private final int COURSE_LEAVE_TYPE = 2;//课时假类型
    private final int OUT_LEAVE_TYPE = 3;//外出假类型

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_cancel_leave);
        initTitleBack(null);
        setTitleText(getString(R.string.cancel_out_leave));
        pullView = (PullToRefreshView) findViewById(R.id.pullview1);
        pullView.setOnHeaderRefreshListener(this);
        pullView.setOnFooterRefreshListener(this);
        cancelLeaveLv = (ListView) findViewById(R.id.lv_cancel_leave);
    }

    @Override
    public void initAfter() {
        showLoadingDialog();
        doGetLeaveList();
    }

    private void refreshView(){
        adapter = new CancelLeaveAdapter(dataList, this);
        cancelLeaveLv.setAdapter(adapter);
        if (dataList.size() == 0) {
            tip("没有请假信息");
        }
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        pullView.onFooterRefreshComplete();
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        doGetLeaveList();
    }

    @Override
    public void cancelBtnClick(int position) {
        final String pkUserActionId = dataList.get(position).getPkUserActionId();
        if (!TextUtils.isEmpty(pkUserActionId)) {
            showTwoBtnDialog(getString(R.string.notice), "确定要撤销这次请假吗", "确定", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showLoadingDialog();
                    doCancelLeave(pkUserActionId);
                }
            }, "取消", null);
        }
    }

    private void doGetLeaveList(){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.GET_USER_ACTION));
        params.addQueryStringParameter("status", OUT_LEAVE_TYPE+"");
        NetUtils.doGetRequest(this, params, new NetTaskListener<WaitApproveResponseBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(WaitApproveResponseBean result) {
                if (null != result.getData()) {
                    dataList = result.getData();
                    refreshView();
                }
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(WaitApproveResponseBean result) {
                tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                cancelLoadingDialog();
                pullView.onHeaderRefreshComplete();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(WaitApproveResponseBean result) {

            }
        });
    }

    /**
     * 销假请求
     * @param pkUserActionId
     */
    private void doCancelLeave(String pkUserActionId){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.CANCEL_LEAVE));
        params.addQueryStringParameter("pkUserActionId", pkUserActionId);
        NetUtils.doGetRequest(this, params, new NetTaskListener<BaseResponseBean>() {

            @Override
            public void onSuccess(BaseResponseBean result) {
                showOneBtnDialog(getString(R.string.notice), "销假完成", getString(R.string.iknow), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HideDialog();
                        showLoadingDialog();
                        doGetLeaveList();
                    }
                });
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(BaseResponseBean result) {
                tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                cancelLoadingDialog();
            }

            @Override
            public void onSuccessCache(BaseResponseBean result) {

            }
        });
    }

    public class WaitApproveResponseBean extends BaseResponseBean implements Serializable {

        List<WaitApproveBean> data;

        public List<WaitApproveBean> getData() {
            return data;
        }

        public void setData(List<WaitApproveBean> data) {
            this.data = data;
        }
    }
}
